﻿define(function (require, exports, module) {
    var m_cache = {};

    exports.getAsync = eval(Wind.compile('async', function (src) {
        if (!m_cache.hasOwnProperty(src))
            $await(exports.addAsync(src));

        return m_cache[src];
    }));

    exports.addAsync = eval(Wind.compile('async', function (src) {
        for (var i = 0; i < 3; i++) {
            var result = $await(loadTask(src));
            if (result.success) {
                m_cache[src] = result.img;
                return true;
            }
        }
        return false;
    }));

    function loadTask(src) {
        return Wind.Async.Task.create(function (t) {
            var img = new Image();
            $(img).load(function () {
                m_cache[src] = img;
                t.complete('success', { success: true, img: img });
            }).error(function () {
                t.complete('success', { success: false });
            }).attr('src', src);
        });
    };
});